<?php
//=============================================================================
// Library
//=============================================================================
function listAllDb()
{
   $dir = 'db';
   if (is_dir($dir)) {
      if ($dh = opendir($dir)) {
        while (($db = readdir($dh)) !== false) {
            if($db !== "." && $db !== "..")echo("<a href=\"index.php?action=selectDb&db_id=$db\">$db</a><br>");
        }
        closedir($dh);
    }
   }
}
function listAllDb2()
{
   $file = "db-list.txt";
   $handle = fopen($file, "r");
   if ($handle) {
    while (($db = fgets($handle)) !== false) {
        $db = trim($db);
        echo("<a href=\"index.php?action=selectDb&db_id=$db\">$db</a><br>");
    }
} else {
    // error opening the file.
}
}

function listAllDbSelect($selected)
{
   $file = "db-list.txt";
   $handle = fopen($file, "r");
   if ($handle) {
    while (($db = fgets($handle)) !== false) {
        $db = trim($db);
        if($db==$selected)echo("<option value=\"$db\" selected>$db</option>");
        else
            echo("<option value=\"$db\">$db</option>");
    }
} else {
    // error opening the file.
}
}

function listAllReq($db)
{
   echo("Requirement List:<br>");
   $dir = 'db/'.$db;
   if (is_dir($dir)) {
      if ($dh = opendir($dir)) {
        while (($req = readdir($dh)) !== false) {
            if($req !== "." && $req !== "..")echo("<a href=\"index.php?action=selectReq&db_id=$db&req_id=$req\">$req</a><br>");
        }
        closedir($dh);
    }
   }
   echo("___<br>");
}

function listAllReq2($db)
{
   $nreq = getCurrentReqId($db);
   echo("Requirement List:<br>");
   $kk = 0;
   for($ii=1;$ii<=$nreq;$ii++)
   {
     echo("<a href=\"index.php?action=selectReq&db_id=$db&req_id=$ii\"> $ii </a>");
     $kk++;
     if($kk == 10)
     {
        $kk = 0;
        echo("<br>");
     }
   }
   echo("<br>___<br>");
}

function showAllReq($chan,$db,$selected)
{
   $nreq = getCurrentReqId($db);
   for($ii=1;$ii<=$nreq;$ii++)
   {
     $file = "db/$db/$ii";
     echo("<div align=\"left\" id=\"$db-$ii\" style=\"float:left; width:100%; background-color:white; color: #0900C4; font: Helvetica 12pt;border: 1px solid green;\">");
     echo("<a href=\"index.php?action=selectReq$chan&db_id=$db&req_id=$ii\">$db-$ii</a>");
     if($ii!=$selected)
       showFileContent($file);
     else
     {
       $text = getReqText($db,$ii);
       $text = trim($text);
       // Add requirement form
       echo("
       <form method=\"post\" action=\"index.php?db_id=$db&req_id=$ii&action=saveReq\" >
       <textarea cols=\"60\" rows=\"5\" name=\"req_text\">
       ");
       echo("($text)");
       echo("
       </textarea>
      <br><input type=\"submit\" value=\"Save\" > 
      </form>
     ");
      echo("<a href=\"index.php?action=link&db_id=$db\"> link </a>");
      echo("<a href=\"index.php?action=deleteReq&db_id=$db&req_id=$selected\"> delete </a>");
      echo("<a href=\"index.php?action=copyReq&db_id=$db\"> copy </a>");
     }
     echo("</div>");
   }

}


function createFile($file,$content)
{
    $handle = fopen($file,"w");
    fwrite($handle,$content);
    fclose($handle);
}


function deleteFile($file)
{

}

function showFileContent($file)
{
    $handle = fopen($file,"r");
    $content = fread($handle, filesize($file));
    fclose($handle);
    echo("<br>$content<br>");
    return;
}
function getFileContent($file)
{
    $handle = fopen($file,"r");
    $content = fread($handle, filesize($file));
    fclose($handle);
    return($content);
}

function createLinkModule($from_db,$to_db)
{
   createFile("db/$from_db/$to_db.link","#Link Module $from_db -> $to_db");
}

function deleteLinkModule($from_db,$to_db)
{

}

function existDb($db)
{
   $res = 0;
   $file = "db-list.txt";
   $handle = fopen($file, "r");
   if ($handle) {
    while (($temp = fgets($handle)) !== false) {
        $temp = trim($temp);
        //$temp = stripslashes($temp);
        //echo("($temp)($db)<br>");
        if("$temp" == "$db")$res = 1;
    }
   } 
   else 
   {
    // error opening the file.
   }
   return($res);
}

function createDb($db)
{
   $res = existDb($db);
   if($res == 0)
   {
     mkdir("db/$db");
     createFile("db/$db.ix","0");
     file_put_contents("db-list.txt", "$db\n", FILE_APPEND | LOCK_EX);
   }
   else
     echo("Database $db already exists<br>");
}

function deleteDb($db)
{

}

function getCurrentReqId($db)
{
    $file = "db/$db.ix";
    $handle = fopen($file,"r");
    $next = fread($handle, filesize($file));
    fclose($handle);
    return($next);
}

function getNextReqId($db)
{
    $file = "db/$db.ix";
    $handle = fopen($file,"r");
    $next = fread($handle, filesize($file));
    fclose($handle);
    echo("$next - ");
    $next++;
    $handle = fopen($file,"w");
    fwrite($handle,$next);
    fclose($handle);
    echo("$next<br>");
    return($next);
}
function saveReq($db,$req,$text)
{
   $fileName = "db/$db/$req";
   echo("($fileName)<br>");
   createFile($fileName,$text);
}

function addReq($db,$text)
{
   $id = getNextReqId($db);
   $fileName = "db/$db/$id";
   createFile($fileName,$text);
}

function removeReq($db,$id)
{
   $fileName = "db/$db/$id";
   deleteFile($fileName);
}

function getReqText($db,$req)
{
    $file = "db/$db/$req";
    $handle = fopen($file,"r");
    $content = fread($handle, filesize($file));
    fclose($handle);
    //echo("$file Content:($content)");
    return($content);
}

function showReqText($db,$req)
{
    $file = "db/$db/$req";
    $handle = fopen($file,"r");
    $content = fread($handle, filesize($file));
    fclose($handle);
    echo("$content");
    return;
}

//=============================================================================

